package cn.img;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

public class Img1 {
    public static void main(String[] args) {
        //建立一个图像
        int w = 300;
        int h = 280;
        //1 10 2
        //(1) 建立内存中的画布
        BufferedImage img = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);

        //(2) 获取画笔
        //Graphics g1 = img.getGraphics();
        //Graphics2D g2 = (Graphics2D)img.getGraphics();
        Graphics2D g = img.createGraphics();
        g.drawString("hello world",30,100);

        Color c = new Color(0,0,255,215);
        g.setColor(c);
        g.fillRect(80,50,100,100);

        //(3)释放画笔占用的内存资源
        g.dispose();

        //(4)保存自己画的img
        try{
            ImageIO.write(img,"jpg",new File("01.jpg"));

            //(5)自动打开图像
            Runtime run = Runtime.getRuntime();
            run.exec("cmd /k start 01.jpg");
        }catch(Exception e){
            e.printStackTrace();
        }


    }
}
